#ifndef XDISK_H
#define XDISK_H

#include "xtypes.h"

//文件系统类型
typedef enum{
    FS_NOT_VALID = 0x00,
    FS_FAT32 = 0x01,
    FS_EXTEND = 0x05,
    FS_WIN95_FAT32_0 = 0x0B,
    FS_WIN95_FAT32_1 = 0x0C,
}xfs_type_t;

//分区表单个表项的信息
#pragma pack(1)
typedef struct _mbr_part_t{
    uint8_t boot_active;

    //物理地址
    uint8_t start_header;
    uint16_t start_sector:6;
    uint16_t start_cylinder:10;
    uint8_t system_id;
    uint8_t end_header;
    uint16_t end_sector:6;
    uint16_t end_cylinder:10;

    uint32_t relative_sectors;    //相对扇区数
    uint32_t total_sectors;
}mbr_part_t;

#define MBR_PRIMART_PART_NR    4

//MBR
typedef struct _mbr_t{
    uint8_t start_code[446];
    mbr_part_t part_info[MBR_PRIMART_PART_NR];
    uint8_t boot_sig;
}mbr_t;

struct _xdisk_t;

//块设备操作接口
typedef struct _xdisk_driver_t{
    xfat_err_t (*open)(struct _xdisk_t* disk, void* init_data);
    xfat_err_t (*close)(struct _xdisk_t* disk);
    xfat_err_t (*read_sector)(struct _xdisk_t* disk, uint8_t* buffer, uint32_t start_sector, uint32_t sector_count);
    xfat_err_t (*write_sector)(struct _xdisk_t* disk, uint8_t* buffer, uint32_t start_sector, uint32_t sector_count);
}xdisk_driver_t;

//块设备参数
typedef struct _xdisk_t{
    const char* name;
    uint32_t sector_size;
    uint32_t total_sector;
    xdisk_driver_t* driver;
    void* data;
}xdisk_t;

//分区信息
typedef struct _xdisk_part_t {
    uint32_t start_sector;
    uint32_t total_sector;
    xfs_type_t type;
    xdisk_t* disk;
}xdisk_part_t;

xfat_err_t xdisk_open(xdisk_t *disk, const char * name, xdisk_driver_t* driver, void *init_data);
xfat_err_t xdisk_close(xdisk_t *disk);
xfat_err_t xdisk_read_sector(xdisk_t *disk, uint8_t *buffer, uint32_t start_sector, uint32_t sector_count);
xfat_err_t xdisk_write_sector(xdisk_t *disk, uint8_t *buffer, uint32_t start_sector, uint32_t sector_count);
xfat_err_t xdisk_get_part_count(xdisk_t*disk, uint32_t *count);
xfat_err_t xdisk_get_part_msg(xdisk_t* disk, xdisk_part_t *xdisk_part, int part_num);

#endif